import pymongo
from pymongo.results import InsertOneResult,InsertManyResult
if __name__ == '__main__':
    mongo = pymongo.MongoClient("mongodb://127.0.0.1:27017/")
    # 切换数据库
    my_db = mongo["my_db"]
    # 切换集合
    my_collection = my_db["my_collection"]

    # 添加一个文档[此处，没有指定_id主键，mongo会自动创建]
    document = {"name": "xiaoming", "mobile": "13012345678", "age": 16}
    ret = my_collection.insert_one(document)
    print(ret.inserted_id)  # 获取返回结果中的新增主键ID
    print(ret.acknowledged) # 执行结果

    # 添加多个文档
    document_list = [
        {"name": "xiaoming", "mobile": "13033345678", "age": 17},
        {"name": "xiaohong", "mobile": "13044345678", "age": 18},
        {"name": "xiaohei", "mobile": "13612345678", "age": 18},
    ]
    ret = my_collection.insert_many(document_list)
    print( ret.inserted_ids ) # 获取返回结果中的新增id列表
    print( ret.acknowledged )

